// ----------------------------------------
// Filename: main.c
// Description: The higest level of the program.
// Author: Jon Maloney
// Date: 9-May-2012
//

//************************************************************
//                    Include .h files
//************************************************************

//************************************************************
//                    Include external references
//************************************************************

#include "hardware_Init.h"
#include "Config_Handler.h"
#include "serial.h"
#include "microRTOS.h"
#include "thread.h"
#include "scheduler.h"
#include "mqueue.h"


//************************************************************
//                    Define Statements
//************************************************************

#define HARDWARE_TEST 1

#if HARDWARE_TEST == 1
	#include "serial.h"
#endif

//************************************************************
//                    Struct, Union, enum Statements
//************************************************************

//************************************************************
//                    Global variables and constants
//************************************************************

//************************************************************
//                    Prototype Private Functions
//************************************************************

void mainThread(void * pvParam);
void vApplicationIdleHook( void );
//************************************************************
//                    Implementation
//************************************************************


/*
 * Create the demo tasks then start the scheduler.
 */
int main( void )
{
	//Sets up hardware fuses etc.
	setupHardware();					

	//Create the first thread needed for the program
	//	this thread will fork any other threads needed
	//	if new configurations are received.	
	Thread_Create(mainThread, NULL, 200, 1);
	
	/* Finally start the scheduler. */
	Scheduler_Start( vApplicationIdleHook );

	/* Will only reach here if there is insufficient heap available to start
	the scheduler. */
	return 0;
}


void mainThread(void * pvParam)
{
	SampleConfigurationType testConfiguration;
	SampleType ADCSample;
	
	//Setup the serial communications
	xComPortHandle pxPort = xSerialPortInitMinimal(57600, 25, (UINT8)sizeof(SampleConfigurationType), (UINT8)sizeof(SampleType)); 

	//Create two queues used for passing new
	//	configuraions to the configuration handler
	//	and receiving samples.
	m_QueueHandle newConfigurations = mqueue_Create(5, sizeof(SampleConfigurationType));
	m_QueueHandle ADC_Samples = mqueue_Create(60, sizeof(SampleType));
	
	//Initialise the configuration handler
	ConfigHandler_Init(newConfigurations, ADC_Samples);
	
	//Create a configuration
	testConfiguration.s.SampleUnit = 2;		//Sample in seconds
	testConfiguration.s.SamplePeriod = 100;	//Sample every 2 sample units
	testConfiguration.s.sampleID = 1; 		//Sample channel 1
	
	//Place the configuration into the new configuration queue
	mqueue_Put(newConfigurations, &testConfiguration, configMAX_DELAY);

	//Create a configuration
	testConfiguration.s.SampleUnit = 2;		//Sample in seconds
	testConfiguration.s.SamplePeriod = 200;	//Sample every 2 sample units
	testConfiguration.s.sampleID = 2; 		//Sample channel 1
	
	//Place the configuration into the new configuration queue
	mqueue_Put(newConfigurations, &testConfiguration, configMAX_DELAY);
	
	//wait for a new ADC value to be placed on the ADC_Sample queue.
	for(;;)
	{
		if(mqueue_Get(ADC_Samples, &ADCSample, configMAX_DELAY) == pdTRUE)
		{
			//Pass the samples straight onto the serial
			while(xSerialPut( pxPort, &ADCSample, configMAX_DELAY ) == pdFALSE)
			{
			}
		}
	}	
}

void vApplicationIdleHook( void )
{
	//Go to sleep here
	for(;;){}
}

